<template>
    <div class="app-container">
        <header-title/>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="关键词" prop="keyword">
                <el-input
                    v-model="queryParams.keyword"
                    placeholder="请输入标题/用户姓名"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="用户" prop="currentUserId">
                <el-select v-model="queryParams.currentUserId" filterable clearable>
                    <el-option
                        v-for="dict in userList"
                        :key="dict.clientUserId"
                        :label="dict.userRealName"
                        :value="dict.clientUserId"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-select v-model="queryParams.type" clearable>
                    <el-option
                        v-for="dict in dict.type.begin_guide_type"
                        :label="dict.label"
                        :value="dict.value"
                        :key="dict.value"
                    ></el-option>
                </el-select>
            </el-form-item>
            
            <el-form-item label="创建时间">
                <el-date-picker
                    style="width: 217px;"
                    v-model="dateRange"
                    size="small"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    type="datetimerange"
                    range-separator="-"
                    :default-time="['00:00:00', '23:59:59']"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                ></el-date-picker>
                </el-form-item>
            <el-form-item class="mar-r-20">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"
                >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">新增</el-button>

            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="tableList">
            <el-table-column label="ID" align="center" prop="beginGuideId" />
            <el-table-column label="标题" align="center" prop="title" min-width="120"/>
            <el-table-column label="教程管理类型" align="center">
                <template slot-scope="scope">
                    {{selectDictLabel(dict.type.begin_guide_type,scope.row.type ) ||'-'}}
                </template>
            </el-table-column>
            <el-table-column label="发布用户姓名" align="center" prop="author"/>
            <el-table-column label="阅读数" align="center" prop="readNum"/>
            <el-table-column label="排序" align="center" prop="order"/>
            <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
            <el-table-column label="操作" align="center" width="180">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-search"
                        @click="handleUpdateDetail(scope.row)"
                    >详情</el-button>
                    <el-button
                        type="text"
                        size="small"
                        icon="el-icon-edit"
                        @click="onHandleUpdate(scope.row)"
                    >编辑</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
        <el-dialog :title="title" :visible.sync="show" width="780px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px" :disabled="type == 'detail' ? true : false">
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item  label="标题" prop="title">
                            <el-input  v-model="form.title" placeholder="请输入标题"  />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="作者名称" prop="author">
                            <el-input  v-model="form.author" placeholder="请输入作者名称"  />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="缩略图" prop="headImg">
                            <imageUpload v-model="form.headImg" size="mini" :isJson="false" :limit="1"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item  label="排序" prop="order">
                            <el-input  v-model="form.order" placeholder="请输入排序"  />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item  label="阅读数" prop="readNum">
                            <el-input  v-model="form.readNum" placeholder="请输入阅读数"  />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="类型" prop="type">
                            <el-select
                                v-model="form.type"
                                placeholder="请选择类型"
                                clearable
                            >
                                <el-option 
                                    v-for="dict in dict.type.begin_guide_type" 
                                    :label="dict.label" 
                                    :key="dict.value" 
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="正文内容" prop="content">
                            <editor v-model="form.content" :min-height="400"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="type != 'detail'" >
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="show = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import {listClientUser} from '@/api/operateManage/userList'
    import { mapGetters } from 'vuex'

    import {listBeginGuide,addBeginGuide,updateBeginGuide,delBeginGuide,detailBeginGuide} from  '@/api/operateManage/beginGuide'
    export default{
        name:'TeachManage',
        dicts: ['tutorial_type','begin_guide_type'],
        data() {
            return {
                // 遮罩层
                loading: true,
                showSearch: true,
                // 总条数
                total: 0,
                // 表格数据
                tableList: [],
                // 查询参数
                dateRange: [],
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    currentUserId: '',
                    isTop:undefined,
                    keyword:'',
                    type:'',
                },
                userList: [],
                show: false,
                form: {},
                type: '',
                title: '',
                rules: {
                    title: [
                        { required: true, message: "标题不能为空", trigger: "blur" }
                    ],
                    type: [
                        { required: true, message: "类型不能为空", trigger: "blur" }
                    ],
                    content: [
                        { required: true, message: "正文内容不能为空", trigger: "blur" }
                    ],
                    readNum: [
                         { required: true, message: "阅读数不能为空", trigger: "blur" }
                    ],
                    order:[
                        { required: true, message: "排序不能为空", trigger: "blur" }
                    ],
                    headImg: [
                        { required: true, message: "缩略图不能为空", trigger: "blur" }
                    ]
                   
                },
                detailId: ''
            }
        },
        computed: {
            ...mapGetters(['userInfo'])
        },
        created() {
            this.getDicts()
            this.getList()
        },
        methods: {
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if(this.detailId){
                            updateBeginGuide(this.form).then(res => {
                                this.$modal.msgSuccess(res.msg)
                                this.show = false
                                this.getList()
                            })
                        }else{
                            addBeginGuide(this.form).then(res => {
                                this.$modal.msgSuccess(res.msg)
                                this.show = false
                                this.getList()
                            })
                        }
                     }
                })
            },
            handleAdd() {
                this.type = 'add'
                this.title = '新增教程'
                this.detailId = ''
                this.form = {
                    title: '',
                    content:'',
                    type: '',
                    userId: this.userInfo.userId,
                    order : 99,
                    readNum:'',
                    author:''
                }
                this.show = true
            },
            handleUpdateDetail(row) {
                this.type = 'detail'
                this.detailId = row.beginGuideId
                this.title = '教程管理详情'
                detailBeginGuide({id: row.beginGuideId}).then(res=> {
                    this.form = res.data
                    this.form.type = Number(res.data.type)
                    this.show = true
                })
            },
            onHandleUpdate(row) {
                this.type = 'edit'
                this.detailId = row.beginGuideId
                this.title = '修改教程管理'
                detailBeginGuide({id: row.beginGuideId}).then(res=> {
                    this.form = res.data
                    this.form.type = Number(res.data.type)
                    this.show = true
                })
            },
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    delBeginGuide(row.tutorialId).then(res => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList()
                    })
                   
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            getDicts() {
                listClientUser({pageSize: 10000,pageNum:1}).then(res=> {
                    this.userList = res.data.records
                })
            },
             /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = []
                this.resetForm("queryForm");
                this.handleQuery();
            },
            getList() {
                this.loading = true;
                 if(this.dateRange && this.dateRange.length){
                    this.queryParams.createTimeStart = this.dateRange[0]
                    this.queryParams.createTimeEnd = this.dateRange[1]
                }else{
                    this.queryParams.createTimeStart = undefined
                    this.queryParams.createTimeEnd = undefined
                }
                listBeginGuide(this.queryParams).then((response) => {
                    this.tableList = response.data.records;
                    this.total = response.data.total;
                    this.loading = false;
                });
            }
        }
    }
</script>